﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using System.ComponentModel;

namespace IronCow.Posh.Commands
{
    [Cmdlet(VerbsCommon.Add, "RtmTask", SupportsShouldProcess = true)]
    [Description("Adds a new RTM task.")]
    public class AddRtmTaskCommand : RtmCmdlet
    {
        [Parameter(
            Mandatory = true,
            Position = 1,
            HelpMessage = "The name of the task.")]
        [ValidateNotNullOrEmpty]
        public string Name { get; set; }

        [Parameter(
            HelpMessage = "The priority of the task.")]
        public TaskPriority Priority { get; set; }

        [Parameter(
            HelpMessage = "The due date of the task.")]
        public string Due { get; set; }

        [Parameter(
            HelpMessage = "The recurrence of the task.")]
        public string Recurrence { get; set; }

        [Parameter(
            HelpMessage = "The estimated time for completing the task.")]
        public string Estimate { get; set; }

        [Parameter(
            HelpMessage = "The task list to put the task in.")]
        public TaskList TaskList { get; set; }

        [Parameter(
            HelpMessage = "The name of the task list to put the task in.")]
        public string TaskListName { get; set; }

        [Parameter(
            HelpMessage = "The tags to apply on the task.")]
        public string[] Tags { get; set; }

        [Parameter(
            HelpMessage = "The URL annotation for the task.")]
        public string Url { get; set; }

        protected override void ProcessRecord()
        {
            Task task = new Task(Name);
            task.Priority = Priority;
            task.Due = Due;
            task.Recurrence = Recurrence;
            task.Estimate = Estimate;
            task.SetTags(Tags);
            task.Url = Url;

            TaskList taskList = TaskList;
            if (taskList == null)
            {
                if (!string.IsNullOrEmpty(TaskListName))
                    taskList = Rtm.TaskLists[TaskListName];
                else
                    taskList = Rtm.GetDefaultTaskList();
            }

            if (ShouldProcess(string.Format("Task: \"{0}\"", task.Name)))
            {
                taskList.AddTask(task);
            }
        }
    }
}
